package com.itheima.test5;

import com.itheima.test3.Student;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("student.txt");

            String str = "";
            int b;
            while ((b = fis.read()) != -1) {
                str += (char) b;
            }
//            System.out.println(str);
            str = str.substring(1, str.length() - 2);
            str = str.replaceAll("Student", "");
            str = str.replaceAll("name", "");
            str = str.replaceAll("gender", "");
            str = str.replaceAll("age", "");
            str = str.replaceAll("score", "");
            str = str.replaceAll(" ", "");
            str = str.replaceAll("'", "");
            str = str.replaceAll("=", "");
//            str = str.replaceAll("}]", "");
//            str = str.replaceAll("'", "");
//            str = str.replaceAll(" ", "");

            String[] split = str.split("},");
            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                String[] split2 = s.split(",");
//                System.out.println(s);
                String name = split2[0].substring(split2[0].indexOf("{") + 1);
                String gender = split2[1];
                int age = Integer.parseInt(split2[2]);
                int score = Integer.parseInt(split2[3]);

                Student stu = new Student(name, gender, age, score);
                list.add(stu);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        for (Student stu : list) {
            System.out.println(stu);
            System.out.println("-----------");
        }
    }
}
